package org.example.service;


import org.example.entity.Dianpu;
import org.example.entity.Gzdiapu;
import org.example.entity.Product;
import org.example.entity.Shanpin;
import org.example.mapper.Dianpumapper;
import org.example.mapper.Shanpinmapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class DianpuService {
    @Autowired
    Dianpumapper dianpumapper;

    @Autowired
    Shanpinmapper shanpinmapper;

    /**
     * 店铺修改
     *
     * @param shopName
     * @param shopDescription
     * @param avatar
     * @param background
     */
    public void dianpuxg(String shopName, String shopDescription,
                         MultipartFile avatar, MultipartFile background, String username) {


        String name = avatar.getOriginalFilename();
        String newname = UUID.randomUUID().toString() + name.substring(name.lastIndexOf("."));


        String name1 = background.getOriginalFilename();
        String newname1;


        Dianpu dianpu = dianpumapper.dianpucx(username);
        if ("placeholder.txt".equals(avatar.getOriginalFilename())
                && avatar.getSize() == 0) {
            newname = dianpu.getAvatar();
        } else {
            try {
                avatar.transferTo(new File("D:/wenjian/java/image/" + newname));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            newname = "http://localhost:8080/image/" + newname;
        }

        if ("placeholder.txt".equals(background.getOriginalFilename())
                && background.getSize() == 0) {
            newname1 = dianpu.getBackground();
        } else {
            newname1 = UUID.randomUUID().toString() + name1.substring(name.lastIndexOf("."));
            try {
                background.transferTo(new File("D:/wenjian/java/image/" + newname1));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            newname1 = "http://localhost:8080/image/" + newname1;
        }

        if (dianpu == null) {

            dianpumapper.dianpuxz(shopName, shopDescription, newname, newname1, username);
        } else {
            dianpumapper.dianpuxg(shopName, shopDescription, newname, newname1, username);

        }
    }

    /**
     * 店铺查询
     *
     * @param username
     * @return
     */
    public Dianpu dianpucx(String username) {
        return dianpumapper.dianpucx(username);
    }

    /**
     * 店铺商品查询
     *
     * @param username
     * @param felename
     * @param souso
     * @return
     */
    public List<Shanpin> shanpincx(String username, String felename, String souso) {
        String regex = ".*" + souso + ".*";
        List<Product> productList = shanpinmapper.productList();
        List<Shanpin> shanpinList = new ArrayList<>();
        for (Product p : productList) {

            Shanpin shanpin = shanpinmapper.cxid(p.getShanpinId());
            if (felename != null && felename != "") {
                if (felename.equals(shanpin.getShanpinfeleName())
                        && username.equals(shanpin.getUsername())
                        && shanpin.getName().matches(regex)) {
                    shanpin.setYingfe(p.getTemplate());
                    shanpinList.add(shanpin);
                }
            } else {
                if (username.equals(shanpin.getUsername()) && shanpin.getName().matches(regex)) {
                    shanpin.setYingfe(p.getTemplate());
                    shanpinList.add(shanpin);
                }
            }
        }
        return shanpinList;
    }


    /**
     * 关注查询
     * @param dianpuusername
     * @param yonghuusername
     * @return
     */
    public Object gzcx(String dianpuusername, String yonghuusername) {

        return dianpumapper.gzcx(dianpuusername,yonghuusername);
    }

    /**
     * 店铺关注修改
     * @param dianpuusername
     * @param yonghuusername
     */
    public void gzxg(String dianpuusername, String yonghuusername) {

        LocalDateTime time=LocalDateTime.now();
        if(gzcx(dianpuusername,yonghuusername)==null){
            dianpumapper.gzxz(dianpuusername,yonghuusername,time);
        }else{
            dianpumapper.gzsc(dianpuusername,yonghuusername);
        }
    }

    /**
     * 关注店铺总查询
     * @param yonghuusername
     * @return
     */
    public List<Dianpu> gzdianpucx(String yonghuusername) {
        Gzdiapu[]dinapuusername=dianpumapper.gzdianpucx(yonghuusername);
        List<Dianpu> dianpuList=new ArrayList<>();
        for(Gzdiapu gz:dinapuusername){
            Dianpu dianpu=dianpumapper.dianpucx(gz.getDianpuusername());
            dianpu.setTime(gz.getGztime());
            dianpu.setGz(false);
            dianpuList.add(dianpu);
        }
        return dianpuList;
    }

    /**
     *店铺总查询
     * @return
     */
    public List<Dianpu> dianpucxs(String souso,String username) {

       List<Dianpu>dianpuList=dianpumapper.dianpucxs(souso);

       for(Dianpu dianpu:dianpuList){
           if(dianpumapper.gzcx(dianpu.getUsername(),username)!=null)
               dianpu.setGz(true);
           else
               dianpu.setGz(false);
       }
       return dianpuList;
    }
}
